﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Media;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;
using MyFloridaHouse.libSvUIControls;
using MyFloridaHouse.libSvUtilities.CustomColors;


namespace MyFloridaHouse.libSvOpenDistricts.MapShapes
{
    public delegate void RegionFillConditionsChangedEventHandler(object sender, EventArgs e);
    public delegate void RegionFillCriteriaChangedEventHandler(object sender, EventArgs e);


    public class RegionFillBase : INotifyPropertyChanged
    {
        #region propertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region RegionFillConditionsChanged
        public event RegionFillConditionsChangedEventHandler RegionFillConditionsChanged;
        public void RaiseRegionFillConditionsChanged(object sender)
        {
            if (RegionFillConditionsChanged != null)
            {
                RegionFillConditionsChanged(sender, new EventArgs());
            }
        }
        #endregion

        #region RegionFillCriteriaChanged
        public event RegionFillCriteriaChangedEventHandler RegionFillCriteriaChanged;
        public void RaiseRegionFillCriteriaChanged(object sender)
        {
            if (RegionFillCriteriaChanged != null)
            {
                RegionFillCriteriaChanged(sender, new EventArgs());
            }
        }
        #endregion

        #region public properties

        private bool _showFill = true;
        public bool showFill
        {
            get { return _showFill; }
            set
            {
                if (_showFill != value)
                {
                    _showFill = value;
                    NotifyPropertyChanged("showFill");
                    RaiseRegionFillConditionsChanged(this);
                }
            }
        }

        private int _opacity = 50;
        public int opacity
        {
            get { return _opacity; }
            set
            {
                if (_opacity != value)
                {
                    _opacity = value; NotifyPropertyChanged("opacity");
                    RaiseRegionFillConditionsChanged(this);
                }
            }
        }

        private ObservableCollection<ColorScaleSection> _colorScaleSectionList;
        public ObservableCollection<ColorScaleSection> colorScaleSectionList
        {
            get { return _colorScaleSectionList; }
            set
            {
                if (_colorScaleSectionList != value)
                {
                    _colorScaleSectionList = value;
                    NotifyPropertyChanged("colorScaleSectionList");
                    RaiseRegionFillConditionsChanged(this);
                }
            }
        }

        private SolidColorBrush _colorScaleMinBrush;
        public SolidColorBrush colorScaleMinBrush
        {
            get { return _colorScaleMinBrush; }
            set 
            {
                _colorScaleMinBrush = value;
                NotifyPropertyChanged("colorScaleMinBrush");
            }

        }

        private SolidColorBrush _colorScaleMaxBrush;
        public SolidColorBrush colorScaleMaxBrush
        {
            get { return _colorScaleMaxBrush; }
            set 
            {
                _colorScaleMaxBrush = value;
                NotifyPropertyChanged("colorScaleMaxBrush"); 
            }
        }

        #endregion
        public RegionFillBase()
        {
            colorScaleMaxBrush = new SolidColorBrush(Colors.Red);
            colorScaleMinBrush = new SolidColorBrush(Colors.Blue);
            LoadColorScaleSelectionList();
        }

        public virtual SolidColorBrush GetRegionFillColorBrush(int regionID, List<RegionDataDTO> regionData)
        {
            throw new NotImplementedException();
        }

        public virtual void AggregateLayerData(int layerID, ObservableCollection<RegionDataDTO> regionsDataList)
        {
            throw new NotImplementedException();
        }


        private void LoadColorScaleSelectionList()
        {
            colorScaleSectionList = new ObservableCollection<ColorScaleSection>();
            for (int pct = 0; pct < 100; pct++)
            {
                colorScaleSectionList.Add(new ColorScaleSection(new SolidColorBrush(RGB.GetColorFromPercent(255, pct)), 2, pct));
            }
        }

        internal SolidColorBrush GetRangedFillBrush(bool criteriaConditionMet, int min, int max, int dataValue)
        {
            SolidColorBrush regionFillColor = new SolidColorBrush(Colors.Transparent);

            // Don't check the include/don't include boolean return, because all we want is the
            // calculated value, we've already determined we want it based on other selected criteria.
            byte alpha = (byte)((double)opacity / 100.0 * (double)255);
            if (criteriaConditionMet)
            {
                // convert data value to a fill color


                double pct = (((float)dataValue - (float)min) / ((float)max - (float)min)) * 100; //convert to percent                    

                regionFillColor = new SolidColorBrush(RGB.GetColorFromPercent(alpha, pct));
            }
            else
            {
                regionFillColor = new SolidColorBrush(Color.FromArgb(alpha, 211, 211, 211));
            }
            return regionFillColor;
        }

        internal SolidColorBrush GetPercentFillBrush(int pct, int regionOpacity)
        {
            byte alpha = (byte)((double)regionOpacity / 100.0 * (double)255);
            return new SolidColorBrush(RGB.GetColorFromPercent(alpha, pct));
        }

    }
}
